package ks.client.gui.admin.user.listeners;


import javax.swing.JFrame;
import javax.swing.JTextField;
import ks.client.gui.ErrorMessageGUI;
import ks.client.gui.admin.user.UserAdminGUI;

/**
 * Removes a user from the UserModel.
 * The user is removed by user name.
 * Used to update the two tables in the UserAdminGUI.
 * 
 * @author Jennifer Hunt
 *
 */
public class RemoveUserController {

	/** UserAdminGUI being managed. */
	UserAdminGUI gui;
	
	/**
	 * Constructor for RemoveUserController.
	 * 
	 * @param userAdminGUI
	 */
	public RemoveUserController(UserAdminGUI userAdminGUI) {
		gui = userAdminGUI;
	}

	/**
	 * Removes a user from the UserModel given a user name.
	 * Produces an error message if the user name field is empty. 
	 * 
	 * @param name
	 */
	public void remove(JTextField name) {
		if(name.getText().isEmpty()){
			JFrame pma = new ErrorMessageGUI("Please type a username.");
			pma.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			pma.setTitle("Error");
			pma.setVisible(true);
		}
		else{
		// update the underlying model.
		UserModel model = gui.getModel();
		model.remove(name.getText());
		
		// make change visible.
		gui.refreshTable();
		}
	}
	
	/**
	 * Removes a user from the UserModel given a user name.
	 * Produces an error message if the user name field is empty. 
	 * Used to manage the table that has a user name and password field.
	 * 
	 * @param name
	 * @param a
	 */
	public void remove(JTextField name, int a) {
		if(name.getText().isEmpty()){
			JFrame pma = new ErrorMessageGUI("Please type a username.");
			pma.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			pma.setTitle("Error");
			pma.setVisible(true);
		}
		else{
			// update the underlying model.
			UserModel model = gui.getModel2();
			model.remove(name.getText());
		
			// make change visible.
			gui.refreshTable();
		}
	}
}
